/* This file is part of MyRename
 * Copyright (C) 2005-2009 Kai-Chieh Ku (kjackie@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#pragma once

#include <stdio.h>

#define UNDO_VER 0

struct UndoFileHeader {
	char identifier[2];
	uint16 version;
	uint32 offset;
};

#define UNDO_READ	0x0000
#define UNDO_WRITE	0x0001

#define READ_OK		0x0000
#define READ_EOF	0x0001
#define READ_FAIL	0x0002

class CUndoFile
{
public:
	CUndoFile();
	CUndoFile(LPCTSTR filename, uint16 mode);
	~CUndoFile();
	bool open(LPCTSTR filename, uint16 mode);
	void close();
	bool WriteHeader();
	bool WriteFooter();
	bool verify();
	bool InitRead();
	int  ReadSession(CString &before, CString &after);
	bool InitWrite(LPCTSTR dir);
	bool WriteSession(LPCTSTR before, LPCTSTR after);
	LPCTSTR GetFilename() const;
	LPCTSTR GetWorkPath() const;
	static LPTSTR bfgets(LPTSTR buf, int len, FILE *stream);
	static int bfputc(TCHAR ch, FILE *stream);
private:
	LPTSTR	fn;
	LPTSTR	workdir;
	UndoFileHeader header;
	FILE	*fh;
	uint32	_mode;
	static char identifier[2];
};
